<?php
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
include_once( "member/proc_betin.php" );
$ui = new UI();
$tpl = new Template2();
$tpl->load( array( "news" => "news.htm" ) );
$conf = new Config();
$news = $conf->Get( "news_th" );
if ( $news == "" )
{
    $news = $conf->Get( "news" );
}
else
{
    $news = "<font color=red>".$news."</font>&#12288;".$conf->Get( "news" );
}
$tpl->set( "newsmessage", $news );
$tpl->show( "news" );
$tpl->set( "account", $curruser['account'] );
if ( $curruser['enabled'] == 0 || $curruser['enabled'] == 2 )
{
    $ui->showErrorPage( "<p class=error>帐号已被暂停使用</p>" );
    exit();
}
$rs = getdb( "select * from lotto where open>='".date( "Y-m-d" )."' order by open" );
if ( $rs->eof )
{
    $ui->showErrorPage( "<p class=error>目前没有可以下注期数！</p>" );
    exit();
}
$open = $rs->f['open'];
$round = $rs->f['round'];
$lotto_id = $rs->f['id'];
$lotto_ns = $rs->f['n1'] + $rs->f['n2'] + $rs->f['n3'] + $rs->f['n4'] + $rs->f['n5'] + $rs->f['n6'];
$stopbet = $conf->get( "waitbet" );
if ( date( "Y-m-d" ) < $open )
{
    exit();
}
else if ( date( "Y-m-d" ) == $open )
{
    if ( $stopbet <= date( "Hi" ) || 0 < $lotto_ns )
    {
        $ui->showErrorPage( "<p>下注期数：".d3( $round )."<br>开奖日：".$open."</p>"."<p class=error>已经截止下注！</p>" );
        exit();
    }
    else if ( date( "Hi" ) <= $conf->get( "startbet" ) )
    {
        exit();
    }
}
$t1 = strtotime( $open." ".$stopbet );
$tpl->Set( "timestap", $t1 - systime( ) );
$tpl->set( "open", $open );
$tpl->set( "round", d3( $round ) );
$tpl->set( "lotto_id", $lotto_id );
$tpl->set( "account", $curruser['account'] );
$tpl->set( "amount", $curruser['amount'] );
$disk = $g_disk_code[getUserValidDisk( $curruser )];
$order = intval( $_POST['order'] );
if ( $order == 0 )
{
    diemsg( "<p class=error>错误的参数！</p>" );
}
$rates = array();
$rs0 = getdb( "select * from rate4".$order.$disk." where lotto_id={$lotto_id}" );
if ( $rs0->eof )
{
    err( "系统目前维护中，请稍後！" );
}
$rates += $rs0->f;
$betmin = $conf->get( "betmin" );
$tpl->set( "min", $betmin );
$betmax = $conf->get( "betmax" );
$tpl->set( "max", $betmax );
$tpl->set( "max_str", $betmax );
$rs = getdb( "select amount_6h from k_user_bet where user_id=".$curruser['id']." and lotto_id=".$lotto_id );
$usedamount = $rs->f[0] ? $rs->f[0] : 0;
$rs->close();
$limit = array();
$limit[25] = GetAmountInfo( 25 );
$ok = isset( $_POST['ok'] );
if ( $ok )
{
    echo "<body>";
}
else
{
    echo "<body onload='StartFocus()'>";
}
echo "<form action=\"op.php?op=member&fp=proc2t\" method=post onsubmit=\"this.submit.disabled=true;this.reset.disabled=true\">";
echo "<input type=hidden name=ok value=1>";
echo "<input type=hidden name=order value={$order}>";
echo "<input type=hidden name=disk value=".strpos( " abcd", $disk ).">";
$usemaxamount = $curruser['usemaxamount'];
$user_rate0 = get_share_rate0( $curruser['id'] );
$u2 = get_user( $curruser['prev_id'] );
$u3 = get_user( $u2['prev_id'] );
$u4 = get_user( $u3['prev_id'] );
$u5 = get_user( $u4['prev_id'] );
$waittime = 2000;
$betcount = 0;
$total = 0;
$tmp = "";
foreach ( $_POST as $key => $val )
{
    $val = intval( $val );
    if ( 0 < $val )
    {
		
		
        $p = explode( "_", $key );
        if ( $key != "ok" && 0 < count( $p ) )
        {
            $p[0] = substr( $p[0], 1 );
            if ( isset( $p[0], $p[1] ) )
            {
                if ( $p[0] == 0 )
                {
                    $p[0] = 25;
                }
				
                $tmp .= "<tr class=td2>";
                $tmp .= "<td>".$g_betname[$p[0]].$g_bignum[$order]."</td>";
                $tmp .= "<td>".$p[1]."</td>";
                ++$betcount;
                $sub_rates = get_subs_rates( $u5['id'], $lotto_id, $p[0], $p[1], $order );
                $rate = $rates["r".$p[1]] - $sub_rates - 0;
                $tmp .= "<td align=right>".trimfloat( $rate, 1 )."</td>";
                $tmp .= "<td align=right>".$val."</td>";
                if ( $ok )
                {
                    $tmp .= "<td>";
                    $betallow = $limit[$p[0]][2];
                    $oneamount = $limit[$p[0]][1];
					$usemaxamount = Get_bet_amounts($p[0], $p[1]);
                    if ( $val < $betmin )
                    {
                        $tmp .= "<font color=red>小於最小限额".$betmin;
                        $waittime += 200;
                    }
                    if ( $curruser['balance'] < $val )
                    {
                        $tmp .= "<font color=red>超过信用额度";
                        $waittime += 200;
                    }
                    else if ( $betallow  < $val + $usemaxamount)
                    {
                        $tmp .= "<font color=red>超过本期限额";
                        $waittime += 200;
                    }
                    else if ( $oneamount < $val )
                    {
                        $tmp .= "<font color=red>超过单注限额".$oneamount;
                        $waittime += 200;
                    }
                    else if ( $rate <= 0 || 50 < $rate )
                    {
                        $tmp .= "<font color=red>赔率数值错误";
                        $waittime += 200;
                    }
                    else
                    {
                        $err = 0;
                        $n = getnumberamount( $curruser['id'], $lotto_id, $p[0], intval( $p[1] ), $order );
                        if ( $betallow < $val + $n )
                        {
                            $err = 1;
                        }
						if ($betallow < $val + $usemaxamount)
                        {
                            $err = 2;
                        }
						
                        if ( $conf->get( "waitbet" ) <= date( "Hi" ) && $err == 0 )
                        {
                            $err = 3;
                        }
                        if ( $err == 1 )
                        {
                            $tmp .= "<font color=red>超过下注限额";
                            $waittime += 200;
                        }
                        else if ( $err == 2 )
                        {
                            $tmp .= "<font color=red>超过下注总额";
                            $waittime += 200;
                        }
                        else if ( $err == 3 )
                        {
                            $tmp .= "<font color=red><b>已经截止下注</b>";
                            $waittime += 200;
                        }
                        else
                        {
                            $stime = systime();
                            if ( betin_2t2b( $p, $val, $rate, $order, $u2, $u3, $u4, $u5, $stime, $disk ) )
                            {
                                $tmp .= "<font color=green>成功 ";
                                $usedamount += $val;
                                $total += $val;
                            }
                            else
                            {
                                $tmp .= "<font color=red>超过信用额度";
                                break;
                            }
                        }
                    }
                    $tmp .= "</font></td>";
                }
                else if ( $p[0] != "" )
                {
                    echo "<input type=hidden name=\"".$key."\" value=\"".$val."\">";
                    $total += $val;
                }
                $tmp .= "</tr>";
            }
        }
    }
}
if ( $betcount == 0 )
{
    $ui->showErrorPage( "<p>请输入下注金额！</p>".( "<p><input type=button value='回上一页' onclick='history.back()'></p>" ) );
    exit();
}
echo "您下注的是：".strtoupper( $disk )." 盘";
echo "<table border=0 cellpadding=2 cellspacing=1 class=table0>";
echo "<tr class=td0><td>下注方式</td><td>下注内容</td><td>赔率</td><td>下注金额</td>";
if ( $ok )
{
    echo "<td>结果</td>";
}
echo "</tr>";
echo $tmp;
echo "<tr class=td0 align=right><td colspan=3>合计</td><td>".$total."</td>".( $ok ? "<td>&nbsp;</td>" : "" )."</tr>";
echo "</table>";
if ( $ok )
{
    echo "<script>";
    echo "parent.leftFrame.location.href='op.php?op=member&fp=account';";
    echo "setTimeout(\"location.href='op.php?op=member&fp=bet2t'\",".$waittime.");";
    echo "</script>";
}
else
{
    echo "<br>";
    echo "<input type=submit name=submit value=确定下注>&#12288;<input name=reset type=reset value=取消下注 onclick='history.back()'>";
    echo "</form>";
    echo "<Script>function StartFocus(){  document.forms[0].submit.focus();}</Script>";
    echo "</body>";
}
?>